package com.hmall.item.web;

import com.hmall.common.dto.PageDTO;
import com.hmall.item.common.DateUtils;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;



@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @GetMapping("/list")
    public PageDTO<Item> list(Integer page, Integer size){
        return itemService.list(page,size);
    }

    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        return itemService.getById(id);
    }

    @PostMapping
    public void save(@RequestBody Item item){

        item.setCreateTime(DateUtils.getNowDate());
        item.setUpdateTime(DateUtils.getNowDate());
        item.setStatus(1);
        if (item.getIsAD()==null){
            item.setIsAD(false);
        }
        itemService.save(item);
    }
    @Transactional
    @PutMapping("/status/{id}/{status}")
    public void statusById(@PathVariable Long id,@PathVariable Integer status){
        Item item = itemService.getById(id);
        item.setStatus(status);
        item.setUpdateTime(DateUtils.getNowDate());
        itemService.updateById(item);
    }
    @PutMapping
    public void update(@RequestBody Item item){
        item.setUpdateTime(DateUtils.getNowDate());
        itemService.updateById(item);
    }
    @DeleteMapping("/{id}")
    public void removeById(@PathVariable Long id){
        itemService.removeById(id);
    }
}
